<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org/">
<head>
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
    <script th:src="@{/static/jquery-2.1.4.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/mylayer.js}" type="text/javascript" charset="utf-8"></script>
    <script th:src="@{/static/kindeditor/kindeditor.js}" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<!--搜索form表单-->
<form class="layui-form layui-row layui-col-space16">
    订单号：
    <div class="layui-inline layui-input-wrap">
        <input type="text" name="name" value="" class="layui-input" lay-affix="clear">
    </div>
    开始创建时间：
    <div class="layui-inline">
        <input type="text" name="beginCreateTime" id="beginCreateTime" lay-verify="datetime" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
    </div>
    结束创建时间：
    <div class="layui-inline">
        <input type="text" name="endCreateTime" id="endCreateTime" lay-verify="datetime" placeholder="yyyy-MM-dd HH:mm:ss" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-inline">
        <input type="checkbox" name="gender" value="1"/>男
        <input type="checkbox" name="gender" value="2"/>女
    </div>
    <div class="layui-inline">
        <button class="layui-btn" lay-submit lay-filter="submitSearch">Search</button>
        <button type="reset" class="layui-btn layui-btn-primary">Clear</button>
    </div>
</form>
<table class="layui-hide" id="test" lay-filter="test"></table>
<!--右侧工具条-->
<script type="text/html" id="barDemo">
    <div class="layui-clear-space">
        <a class="layui-btn layui-btn-xs" lay-event="reset">查看详情</a>
    </div>
</script>
<!--table中展示status的templet-->
<!--<script type="text/html" id="statusTemplet">
    <input type="checkbox" name="status" value="{{= d.id }}" title="正常|停用" lay-skin="switch"
           lay-filter="statusFilter" {{= d.status == 0 ? "checked" : "" }}>
</script>-->
<script type="text/html" id="statusTemplet">
    <input type="checkbox" name="status" value="{{= d.id }}" title="正常|停用" lay-skin="switch" }}>
</script>

<!--订单状态-->
<script type="text/html" id="statusTemplet2">
    {{# if(d.status === 0){ }}
    <span style="color: #000000;">已取消</span>
    {{# } else if(d.status === 10) { }}
    <span style="color: #FF5722;">未付款</span>
    {{# } else if(d.status === 20) { }}
    <span style="color: #afe6ad;">已付款</span>
    {{# } else if(d.status === 30) { }}
    <span style="color: #ffa640;">已发货</span>
    {{# } else if(d.status === 40) { }}
    <span style="color: #16b777;">已完成</span>
    {{# } else if(d.status === 50) { }}
    <span style="color: #12ff02;">交易成功</span>
    {{# } else if(d.status === 60) { }}
    <span style="color: #000000;">交易关闭</span>
    {{# } }}
</script>

<script>
    layui.use(['table', 'form', 'upload','laydate'], function () {
        var form = layui.form;
        var table = layui.table;
        var laydate = layui.laydate;

        // 状态 - 开关操作
        form.on('switch(statusFilter)', function(obj){
            var id = this.value;
            var checked = obj.elem.checked;
            console.log(checked);
            var status = checked ? 1 : 0;
            $.post(
                '/user?method=updateStatus',
                {'id': id, 'status': status},
                function (result) {
                    if (result.code == 0) {
                        mylayer.okMsg(result.msg);
                    }
                },
                'json'
            );
        });

        // 日期
        laydate.render({
            elem: '#beginCreateTime',
            type: 'datetime'
        });
        laydate.render({
            elem: '#endCreateTime',
            type: 'datetime'
        });

        // 创建渲染实例
        table.render({
            elem: '#test',
            id: 'tableId',
            url: '/orderitem/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'id', title: 'ID', width: 50},
                    {field: 'orderNo', title: '订单号', width: 175},
                    {field: 'payment', title: '实际付款', width: 90},
                    {field: 'paymentTime', title: '支付时间', width: 165},
                    {field: 'paymentType', title: '支付方式', width: 90},
                    {field: 'createTime', title: '创建时间',width: 165},
                    {field: 'status', title: '状态', templet:'#statusTemplet2', width: 90},
                    {field: 'productId', title: '商品ID', width: 70},
                    {field: 'productName', title: '商品名称', width: 90},
                    {field: 'currentUnitPrice', title: '单价', width: 60},
                    {field: 'quantity', title: '数量', width: 60},
                    {field: 'totalPrice', title: '总价', width: 60},
                    {fixed: 'right', title: '操作', width: 134, minWidth: 125, toolbar: '#barDemo'}
                ]
            ],
            page: true
        });

        // 搜索提交
        form.on('submit(submitSearch)', function (data) {
            var field = data.field; // 获得表单字段
            // 执行搜索重载
            table.reload('tableId', {
                page: {
                    curr: 1 // 重新从第 1 页开始
                },
                where: field // 搜索的字段
                //http://localhost:8080/product?method=selectByPage&page=1&limit=10&name=zhansgsan&address=5&time=2014
            });
            return false; // 阻止默认 form 跳转
        });



        // 触发单元格工具事件
        table.on('tool(test)', function (obj) { // 双击 toolDouble
            var data = obj.data; // 获得当前行数据
            console.log(obj);
            //{"id": 1,"name": "Java1807","address": "dfdf","time": "3434"}
            console.log(obj.data);
            if (obj.event === 'reset') {
                layer.confirm(
                    '确认要重置密码么?',
                    {icon: 3},
                    function (index) {
                        $.post(
                            '/product/resetpassword',
                            {'id': data.id},
                            function (result) {
                                console.log(result);
                                if (result.code == 0) {
                                    mylayer.okMsg(result.msg);
                                    //删除成功之后刷新表格，展示最新数据
                                    table.reload('tableId');
                                } else {
                                    mylayer.errorMsg(result.msg);
                                }
                            },
                            'json'
                        );
                    }
                );
            }
        });

    });
</script>
</body>
</html>